/* Copyright (c) 2011 by Mani Brundha Kattamuri, Haritha Subramanyam
 *
 * Project Info:  http://code.google.com/p/andriod-travelapp-csc780/wiki/Design
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */
package com.example.TravelApp.search.quick;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.example.TravelApp.R;
import com.example.TravelApp.constants.MapConstants;
import com.example.TravelApp.search.common.MapListOfOptionsActivity;
import com.example.TravelApp.search.common.PlaceObject;
import com.example.TravelApp.search.common.helper.JSONParseHelper;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

/**
 * This activity is called when the user clicks on the go button in the
 * QuickSearchActivity Two tabs are displayed to the user 1. Displays details of
 * the list of suggested places 2. Displays all the suggested places as points
 * on the map.
 * 
 * @author Haritha Subramanyam, Mani Brundha
 * 
 */
public class TabOptionDetailsMapActivity extends TabActivity implements
		OnTabChangeListener, MapConstants {
	/** Called when the activity is first created. */
	String place, interest;
	TabHost host;
	private List<PlaceObject> list = new ArrayList<PlaceObject>();;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Resources r = getResources();
		host = getTabHost();
		place = (String) getIntent().getCharSequenceExtra("Place");
		interest = (String) getIntent().getCharSequenceExtra("Intrest");

		JSONObject json = JSONParseHelper.getLocationInfo(place,
				GOOGLE_PLACES_RADIUS_VALUE, interest, GOOGLE_PLACES_NAME_VALUE,
				GOOGLE_PLACES_SENSOR_VALUE);
		this.list = JSONParseHelper.getList(json);

		// Tab displaying details of the list of suggested places.
		host.addTab(host
				.newTabSpec("Detailed Options")
				.setIndicator("Detailed Options",
						r.getDrawable(R.drawable.tab_bg))
				.setContent(
						new Intent(this, EachSubOptionDetailActivity.class)
								.putParcelableArrayListExtra("placesList",
										(ArrayList<? extends Parcelable>) list)
								.putExtra("Place", place)));
		host.getTabWidget().getChildAt(0).getLayoutParams().height = 55;

		// Tab displaying all the suggested places as points on the map.
		host.addTab(host
				.newTabSpec("Map")
				.setIndicator("Map", r.getDrawable(R.drawable.tab_bg))
				.setContent(
						new Intent(this, MapListOfOptionsActivity.class)
								.putParcelableArrayListExtra("placesList",
										(ArrayList<? extends Parcelable>) list)));
		host.getTabWidget().getChildAt(1).getLayoutParams().height = 55;

		// set listener to tabhost IMPORTANT
		host.setOnTabChangedListener(this);

		tabStyling(); // changes tab styles

	}

	/**
	 * Modifies the styling of the tabs when the tab change activity occurs
	 */
	@Override
	public void onTabChanged(String tabId) {
		for (int i = 0; i < host.getTabWidget().getChildCount(); i++) {
			host.getTabWidget().getChildAt(i)
					.setBackgroundResource(R.drawable.tab_bg);

		}
		host.getTabWidget().getChildAt(host.getCurrentTab())
				.setBackgroundResource(R.drawable.tab_bg);
	}

	/**
	 * Adds custom styling to the tab by using drawable resources
	 */
	private void tabStyling() {
		// change tab background color to red
		for (int i = 0; i < host.getTabWidget().getChildCount(); i++) {
			host.getTabWidget().getChildAt(i)
					.setBackgroundResource(R.drawable.tab_bg);
		}
		// Sets different color(blue) for current selected tab
		host.getTabWidget().getChildAt(host.getCurrentTab())
				.setBackgroundResource(R.drawable.tab_bg);

	}
}